package com.grain.security.rbac.repository.support;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenericUtils {
    public static Class getGenericClass(Class clazz) {
        return  getGenericClass(clazz,0);
    }

    private static Class getGenericClass(Class clazz, int i) {
        Type t = clazz.getGenericSuperclass();
        if (t instanceof ParameterizedType) {
            Type[] params = ((ParameterizedType) t).getActualTypeArguments();
            if (params[i] instanceof ParameterizedType) {
                return ((ParameterizedType) params[i]).getRawType().getClass();
            } else {
                return (Class) params[i];
            }
        }
        throw new RuntimeException("无法获得泛型的类型");
    }
}
